package com.example.back.dao;

import com.example.back.bean.UserInfo;
import com.example.back.bean.user.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface UserDao {
    @Select("select * from user_inf order by userId desc")
    public List<UserInfo> getAll();

    @Update("update user_inf set freeCards = freeCards + #{freeCards},whetherPay=1 where userId = #{userId} ")
    void updateUserFreeCardsInfo(long userId, long freeCards);

    @Select("select * from user_inf where userId = #{userId}")
    public UserInfo getOne(long userId);

    @Select("select * from t_first_charge limit 1")
    Map<Object, Object> selectFirstCharge();

    @Select("select * from t_resources_configs where msgType=#{msgType} and msgKey=#{msgKey}")
    Map<String, Object> luckyDiscountMap(String msgType, String msgKey);

    @Update("update user_inf set freeCards = freeCards + #{num} where userId = #{userId}")
    public int update(long num, long userId);

    @Update("update user_inf set freeCards = freeCards + #{num},discountType=2 where userId = #{userId}")
    public int updateLucky(long num, long userId);

    @Update("update user_inf set diamond= diamond+#{diamond} where userId=#{userId}")
    public int updateUserDiamond(long userId,long diamond);

    @Update("update user_inf set diamond= diamond+#{diamond},whetherPay=1 where userId=#{userId}")
    public int updateShowUserDiamond(long userId, int diamond);

//    @Select("select jk.userId,jk.name,jk.freeCards,jk.diamond,jk.totalCount,jk.topupMoney,jk.adsCount,jk.loginDays,jk.logTime,jk.logoutTime\n" +
//            ",jk.regTime,jk.isOnLine,jk.`level`,jk.strength from (select a.userId,a.name,a.freeCards,a.diamond,a.totalCount,a.isOnLine,\n" +
//            "            a.loginDays,a.regTime,a.logTime,a.logoutTime,a.topupMoney,count(user_ads_record.userId) as adsCount ,a.`level`,a.strength\n" +
//            "             FROM (SELECT user_inf.userId,name,freeCards,diamond,totalCount,isOnLine,loginDays,regTime,logoutTime,logTime,\n" +
//            "             sum(amount) as topupMoney,`level`,strength  FROM user_inf  LEFT JOIN p_pay_order  \n" +
//            "            on user_inf.userId=p_pay_order.userId and status =1 GROUP BY user_inf.userId)as a \n" +
//            "            LEFT JOIN user_ads_record on a.userId=user_ads_record.userId  GROUP BY a.userId)as jk\n" +
//            "            where jk.isOnLine=#{isOnline}")
    @Select("select lo.userId,lo.name,lo.freeCards,lo.diamond,lo.totalCount,lo.topupMoney,lo.adsCount,lo.loginDays,lo.logTime,lo.logoutTime,lo.regTime,lo.isOnLine,lo.`level`,lo.strength,if(endTime is null,'否','是')as monCard,lo.passCard\n" +
            "            from \n" +
            "            (select jk.userId,jk.name,jk.freeCards,jk.diamond,jk.totalCount,jk.topupMoney,jk.adsCount,jk.loginDays,jk.logTime,jk.logoutTime,\n" +
            "            jk.regTime,jk.isOnLine,jk.`level`,jk.strength,jk.passCard from\n" +
            "            (select a.userId,a.name,a.freeCards,a.diamond,a.totalCount,a.isOnLine,a.passCard,\n" +
            "            a.loginDays,a.regTime,a.logTime,a.logoutTime,a.topupMoney,count(user_ads_record.userId) as adsCount ,a.`level`,a.strength \n" +
            "            FROM (SELECT user_inf.userId,name,freeCards,diamond,totalCount,isOnLine,loginDays,regTime,logoutTime,logTime, sum(amount) \n" +
            "            as topupMoney,`level`,strength,passCard  FROM user_inf  LEFT JOIN p_pay_order  on user_inf.userId=p_pay_order.userId \n" +
            "            and status =1 GROUP BY user_inf.userId)as a LEFT JOIN user_ads_record on a.userId=user_ads_record.userId  GROUP BY a.userId)as jk where jk.isOnLine=#{isOnline} ) as lo LEFT JOIN\n" +
            "            t_monthly_card on lo.userId = t_monthly_card.userId and endTime>NOW() ;")
    public List<User> getIsOnline(int isOnline);


//    @Select("select jk.userId,jk.name,jk.freeCards,jk.diamond,jk.totalCount,jk.topupMoney,jk.adsCount,jk.loginDays,jk.logTime,jk.logoutTime\n" +
//            ",jk.regTime,jk.isOnLine,jk.`level`,jk.strength from (select a.userId,a.name,a.freeCards,a.diamond,a.totalCount,a.isOnLine,\n" +
//            "            a.loginDays,a.regTime,a.logTime,a.logoutTime,a.topupMoney,count(user_ads_record.userId) as adsCount ,a.`level`,a.strength\n" +
//            "             FROM (SELECT user_inf.userId,name,freeCards,diamond,totalCount,isOnLine,loginDays,regTime,logoutTime,logTime,\n" +
//            "             sum(amount) as topupMoney,`level`,strength  FROM user_inf  LEFT JOIN p_pay_order  \n" +
//            "            on user_inf.userId=p_pay_order.userId and status =1 GROUP BY user_inf.userId)as a \n" +
//            "            LEFT JOIN user_ads_record on a.userId=user_ads_record.userId  GROUP BY a.userId)as jk\n" +
//            "where jk.regTime BETWEEN #{startTime} AND #{endTime} order by jk.regTime desc ")
    @Select("select lo.userId,lo.name,lo.freeCards,lo.diamond,lo.totalCount,lo.topupMoney,lo.adsCount,lo.loginDays,lo.logTime,lo.logoutTime,lo.regTime,lo.isOnLine,lo.`level`,lo.strength,if(endTime is null,'否','是')as monCard,lo.passCard\n" +
            "            from \n" +
            "            (select jk.userId,jk.name,jk.freeCards,jk.diamond,jk.totalCount,jk.topupMoney,jk.adsCount,jk.loginDays,jk.logTime,jk.logoutTime,\n" +
            "            jk.regTime,jk.isOnLine,jk.`level`,jk.strength,jk.passCard from\n" +
            "            (select a.userId,a.name,a.freeCards,a.diamond,a.totalCount,a.isOnLine,a.passCard,\n" +
            "            a.loginDays,a.regTime,a.logTime,a.logoutTime,a.topupMoney,count(user_ads_record.userId) as adsCount ,a.`level`,a.strength \n" +
            "            FROM (SELECT user_inf.userId,name,freeCards,diamond,totalCount,isOnLine,loginDays,regTime,logoutTime,logTime, sum(amount) \n" +
            "            as topupMoney,`level`,strength,passCard  FROM user_inf  LEFT JOIN p_pay_order  on user_inf.userId=p_pay_order.userId \n" +
            "            and status =1 GROUP BY user_inf.userId)as a LEFT JOIN user_ads_record on a.userId=user_ads_record.userId  GROUP BY a.userId)as jk where jk.regTime BETWEEN #{startTime} AND #{endTime} ) as lo LEFT JOIN\n" +
            "            t_monthly_card on lo.userId = t_monthly_card.userId and endTime>NOW() ;")
    public List<User> getRegUser(Date startTime, Date endTime);

//    @Select("select jk.userId,jk.name,jk.freeCards,jk.diamond,jk.totalCount,jk.topupMoney,jk.adsCount,jk.loginDays,jk.logTime,jk.logoutTime,\n" +
//            "            jk.regTime,jk.isOnLine,jk.`level`,jk.strength from (select a.userId,a.name,a.freeCards,a.diamond,a.totalCount,a.isOnLine,\n" +
//            "            a.loginDays,a.regTime,a.logTime,a.logoutTime,a.topupMoney,count(user_ads_record.userId) as adsCount ,a.`level`,a.strength \n" +
//            "            FROM (SELECT user_inf.userId,name,freeCards,diamond,totalCount,isOnLine,loginDays,regTime,logoutTime,logTime, sum(amount) \n" +
//            "            as topupMoney,`level`,strength  FROM user_inf  LEFT JOIN p_pay_order  on user_inf.userId=p_pay_order.userId \n" +
//            "            and status =1 GROUP BY user_inf.userId)as a LEFT JOIN user_ads_record on a.userId=user_ads_record.userId  GROUP BY a.userId)as jk")
    @Select("select lo.userId,lo.name,lo.freeCards,lo.diamond,lo.totalCount,lo.topupMoney,lo.adsCount,lo.loginDays,lo.logTime,lo.logoutTime,lo.regTime,lo.isOnLine,lo.`level`,lo.strength,if(endTime is null,'否','是')as monCard,lo.passCard\n" +
            "            from \n" +
            "            (select jk.userId,jk.name,jk.freeCards,jk.diamond,jk.totalCount,jk.topupMoney,jk.adsCount,jk.loginDays,jk.logTime,jk.logoutTime,\n" +
            "            jk.regTime,jk.isOnLine,jk.`level`,jk.strength,jk.passCard from\n" +
            "            (select a.userId,a.name,a.freeCards,a.diamond,a.totalCount,a.isOnLine,\n" +
            "            a.loginDays,a.regTime,a.logTime,a.logoutTime,a.topupMoney,count(user_ads_record.userId) as adsCount ,a.`level`,a.strength,a.passCard \n" +
            "            FROM (SELECT user_inf.userId,name,freeCards,diamond,totalCount,isOnLine,loginDays,regTime,logoutTime,logTime, sum(amount) \n" +
            "            as topupMoney,`level`,strength,passCard  FROM user_inf  LEFT JOIN p_pay_order  on user_inf.userId=p_pay_order.userId \n" +
            "            and status =1 GROUP BY user_inf.userId)as a LEFT JOIN user_ads_record on a.userId=user_ads_record.userId  GROUP BY a.userId)as jk) as lo LEFT JOIN\n" +
            "            t_monthly_card on lo.userId = t_monthly_card.userId and endTime>NOW();")
    List<User> getAllUserInfo();

    @Select("select lo.userId,lo.name,lo.freeCards,lo.diamond,lo.totalCount,lo.topupMoney,lo.adsCount,lo.loginDays,lo.logTime,lo.logoutTime,lo.regTime,lo.isOnLine,lo.`level`,lo.strength,if(endTime is null,'否','是')as monCard,lo.passCard \n" +
            "            from\n" +
            "            (select jk.userId,jk.name,jk.freeCards,jk.diamond,jk.totalCount,jk.topupMoney,jk.adsCount,jk.loginDays,jk.logTime,jk.logoutTime,\n" +
            "             jk.regTime,jk.isOnLine,jk.`level`,jk.strength,jk.passCard from\n" +
            "            (select a.userId,a.name,a.freeCards,a.diamond,a.totalCount,a.isOnLine,\n" +
            "            a.loginDays,a.regTime,a.logTime,a.logoutTime,a.topupMoney,count(user_ads_record.userId) as adsCount ,a.`level`,a.strength,a.passCard \n" +
            "            FROM (SELECT user_inf.userId,name,freeCards,diamond,totalCount,isOnLine,loginDays,regTime,logoutTime,logTime, sum(amount) \n" +
            "            as topupMoney,`level`,strength,passCard  FROM user_inf  LEFT JOIN p_pay_order  on user_inf.userId=p_pay_order.userId \n" +
            "            and status =1 GROUP BY user_inf.userId)as a LEFT JOIN user_ads_record on a.userId=user_ads_record.userId GROUP BY a.userId)as jk where jk.userId like concat(#{userId},'%') ) as lo LEFT JOIN\n" +
            "            t_monthly_card on lo.userId = t_monthly_card.userId and endTime>NOW()")
    List<User> getOneUserInfo(long userId);
}
